﻿using Interface.Dapper;
using Interface.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface.Core
{
    public class DataContext : IDisposable
    {
        private static readonly string ScrmConnString = ConfigHelper.ConnectionString("ScrmConnString");
        private static readonly string ScrmProviderName = ConfigHelper.ProviderName("ScrmConnString");
        private static readonly string ErpConnString = ConfigHelper.ConnectionString("ErpConnString");
        private static readonly string ErpProviderName = ConfigHelper.ProviderName("ErpConnString");
        private static readonly string CrmConnString = ConfigHelper.ConnectionString("CrmConnString");
        private static readonly string CrmProviderName = ConfigHelper.ProviderName("CrmConnString");
        private static readonly string EngOAConnString = ConfigHelper.ConnectionString("EngOAConnString");
        private static readonly string EngOAProviderName = ConfigHelper.ProviderName("EngOAConnString");
        private static readonly string sksConnString = ConfigHelper.ConnectionString("sksConnString");
        private static readonly string sksProviderName = ConfigHelper.ProviderName("sksConnString");

        private DapperContext scrmContext = null;
        private DapperContext erpContext = null;
        private DapperContext crmContext = null;
        private DapperContext engoaContext = null;
        private DapperContext sksContext = null;

        public DapperContext Scrm
        {
            get
            {
                if (scrmContext == null)
                {
                    scrmContext = new DapperContext(ScrmConnString, ScrmProviderName);
                }
                return scrmContext;
            }
        }
        public DapperContext Erp
        {
            get
            {
                if (erpContext == null)
                {
                    erpContext = new DapperContext(ErpConnString, ErpProviderName);
                }
                return erpContext;
            }
        }
        public DapperContext Crm
        {
            get
            {
                if (crmContext == null)
                {
                    crmContext = new DapperContext(CrmConnString, CrmProviderName);
                }
                return crmContext;
            }
        }
        public DapperContext EngOA
        {
            get
            {
                if (engoaContext == null)
                {
                    engoaContext = new DapperContext(EngOAConnString, EngOAProviderName);
                }
                return engoaContext;
            }
        }
        public DapperContext sks
        {
            get
            {
                if (sksContext == null)
                {
                    sksContext = new DapperContext(sksConnString, sksProviderName);
                }
                return sksContext;
            }
        }

        public DataContext()
        {
            DapperConfig.MapperInit();
        }

        /// <summary>
        /// 异常情况下使用的回滚
        /// </summary>
        public void Rollback()
        {
            if (scrmContext != null)
            {
                scrmContext.Rollback();
            }
            if (crmContext != null)
            {
                crmContext.Rollback();
            }
            if (engoaContext != null)
            {
                engoaContext.Rollback();
            }
        }

        #region 释放资源

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Dispose(true); GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否释放</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (scrmContext != null) { try { scrmContext.Dispose(); scrmContext = null; } catch { } }
                if (crmContext != null) { try { crmContext.Dispose(); crmContext = null; } catch { } }
                if (engoaContext != null) { try { engoaContext.Dispose(); engoaContext = null; } catch { } }
            }
        }

        #endregion
    }
}